var app = getApp();
var WxParse = require('../../wxParse/html2json.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    // tab切换
    currentTab: 0,
    orderPayment: 0,
    orderEvaluate: 0,
    orderDelivery: 0,
    orderReceipt: 0,
    orderList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    })
  },

  myRequest:function(state){
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        let openid = res.data
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: app.globalData.URL + '/app/personalOrder/orderList?state=' + state +'&openid=' + openid,
          success: function (res) {
            console.log(res.data)
            wx.hideLoading()
            that.setData({
              orderList: res.data.orderList,
              paseImg: that.parseImg(res.data.orderList, that),

              orderPayment: res.data.orderCount.orderPayment || 0,
              orderEvaluate: res.data.orderCount.orderDaifahuo || 0,
              orderDelivery: res.data.orderCount.orderDelivery || 0,
              orderReceipt: res.data.orderCount.orderReceipt || 0,
            })
          }
        })
      },
    })
  },

  onShow:function(){    
    this.myRequest("")
    this.setData({
      currentTab: 0
    })
  },


  // 滑动切换tab
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },

  // 点击tab切换
  swichNav: function (e) {
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    if (that.data.currentTab == 0) {
      that.myRequest("")
      
    } else if (that.data.currentTab == 1) {
      that.myRequest(0)
      
    } else if (that.data.currentTab == 2) {
      that.myRequest(1)
      
    } else if (that.data.currentTab == 3) {
      that.myRequest(2)

    } else if (that.data.currentTab == 4) {
      that.myRequest(3)      
    }
  },


  //已支付等待发货
  toWaitFH: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../waitFH/waitFH?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type,
    })
  },
  //待支付等待付款
  toWaitFK:function(e){
    console.log(e)
    wx.navigateTo({
      url: '../waitFK/waitFK?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type,
    })
  },
  //已发货等待收货
  toWaitSH:function(e){
    console.log(e)
    wx.navigateTo({
      url: '../waitFK/waitFK?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type,
    })
  },

  toDetail:function(e){
    let state = e.currentTarget.dataset.state
    if(state == '0'){
      wx.navigateTo({
        url: '../waitFK/waitFK?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type,
      })
    }else if(state == '1'){
      wx.navigateTo({
        url: '../waitFH/waitFH?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type,
      })
    } else if (state == '2') {
      wx.navigateTo({
        url: '../waitFK/waitFK?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type,
      })
    }else{
      wx.navigateTo({
        url: '../waitFK/waitFK?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type,
      })
    }
  },
  toPay: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../pay/pay?id=' + e.currentTarget.id + '&state=order',
    })
  },
  makeSure: function (e) {
    console.log(e)
    var that = this
    wx.getStorage({ 
      key: 'openid',
      success: function(res) {
        let openid = res.data
        
        wx.request({
          url: app.globalData.URL + '/app/personalOrder/goodsReceipt?id=' + e.currentTarget.dataset.id + '&openid=' + openid,
          success: function (res) {
            console.log(res.data)
            if (res.data.flag == true) {
              wx.showToast({
                title: '收货成功',
                success: function () {
                  that.myRequest(2)
                }
              })
            }
          }
        })
      },
    })
    
  },

  //解析富文本
  parseImg: function (data, that) {
    var parseImgObj = {}

    for(var j=0; j<data.length; j++){
      var proList = data[j].proList || []

      for (var i = 0; i < proList.length; i++) {
        parseImgObj[proList[i].orderId] = WxParse.html2json(proList[i].orderClassPic, 'returnData');
      }
    }
    
    return parseImgObj
  },

  wuliu:function(e){
    wx.navigateTo({
      url: '../wuliu/wuliu?giveMode=' + e.currentTarget.dataset.giveMode + '&expressNumber=' + e.currentTarget.dataset.expressNumber,
    })
  }
})